package tan.dft.gps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tan.dft.quanly.Quanly;
import tan.dft.quanly.QuanlyAdapter;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.ListView;

public class Danhsachquanly extends Activity {

	private String myjsonstring, add;
	private Geocoder geocoder;
	private int idmayintent, idql1;
	private List<Address> addresses;
	private ListView listquanly;
	private ArrayList<Quanly> array;
	private QuanlyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.danhsachquanly);

		listquanly = (ListView) findViewById(R.id.listdanhsachquanly);
		array = new ArrayList<Quanly>();
		adapter = new QuanlyAdapter(Danhsachquanly.this, R.layout.quanlyitem,
				array);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		idmayintent = (Integer) bundle.getInt("idmay");

		creatArray();
		listquanly.setAdapter(adapter);

	}

	private void creatArray() {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(getAssets().open(
					"jsondata.txt")));
			String temp;
			while ((temp = br.readLine()) != null)
				sb.append(temp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		myjsonstring = sb.toString();
		try {
			JSONObject jsonObjMain = new JSONObject(myjsonstring);
			JSONArray jsonArray = jsonObjMain.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObj = jsonArray.getJSONObject(i);
				int idmay = (Integer) jsonObj.getInt("idmay");
				if (idmay == idmayintent) {
					JSONArray jsonArrayql = jsonObj.getJSONArray("idql");
					for (int j = 0; j < jsonArrayql.length(); j++) {
						JSONObject jsonObjectql = jsonArrayql.getJSONObject(j);
						idql1 = jsonObjectql.getInt("idql1");
						String name = jsonObjectql.getString("name");
						String gender = jsonObjectql.getString("gender");
						int age = jsonObjectql.getInt("age");
						JSONObject jObj = jsonObjectql.getJSONObject("toado");
						Double lat = jObj.getDouble("lat");
						Double lng = jObj.getDouble("lng");

						geocoder = new Geocoder(this, Locale.getDefault());
						try {
							addresses = geocoder.getFromLocation(lat, lng, 1);
							String address = addresses.get(0).getAddressLine(0);
							String city1 = addresses.get(0).getAddressLine(1);
							String country = addresses.get(0).getAddressLine(2);
							add = address + " | " + city1 + " | " + country;

						} catch (IOException e) {
							e.printStackTrace();
						}

						Quanly quanly = new Quanly(idmay, idql1, age, name,
								add, gender, lat, lng);
						array.add(quanly);

					}

				}

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
